package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.logi.sys.bo.LogiPickupWaybillEditBo;
import com.piggy.logi.sys.bo.LogiPickupWaybillQueryBo;
import com.piggy.logi.sys.domain.LogiPickupWaybill;
import com.piggy.logi.sys.mapper.LogiPickupWaybillMapper;
import com.piggy.logi.sys.service.ILogiPickupWaybillService;
import com.piggy.logi.sys.vo.LogiPickupWaybillVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 提货单运单关联关系Service业务层处理
 *
 * @author piggy
 * @date 2023-08-25
 */
@Slf4j
@Service
public class LogiPickupWaybillServiceImpl extends ServiceImpl<LogiPickupWaybillMapper, LogiPickupWaybill> implements ILogiPickupWaybillService {

    @Override
    public LogiPickupWaybillVo queryById(Long id) {
        return getVoById(id, LogiPickupWaybillVo.class);
    }

    @Override
    public TableDataInfo<LogiPickupWaybillVo> queryPageList(LogiPickupWaybillQueryBo bo) {
        PagePlus<LogiPickupWaybill, LogiPickupWaybillVo> result = pageVo(PageUtils.buildPagePlus(bo, true), buildQueryWrapper(bo), LogiPickupWaybillVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiPickupWaybillVo> queryList(LogiPickupWaybillQueryBo bo) {
        return listVo(buildQueryWrapper(bo), LogiPickupWaybillVo.class);
    }

    private LambdaQueryWrapper<LogiPickupWaybill> buildQueryWrapper(LogiPickupWaybillQueryBo bo) {
        LambdaQueryWrapper<LogiPickupWaybill> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, LogiPickupWaybill::getId, bo.getId());
        lqw.eq(bo.getDeptId() != null, LogiPickupWaybill::getDeptId, bo.getDeptId());
        lqw.eq(bo.getPickupOrderId() != null, LogiPickupWaybill::getPickupOrderId, bo.getPickupOrderId());
        lqw.eq(bo.getWaybillId() != null, LogiPickupWaybill::getWaybillId, bo.getWaybillId());
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiPickupWaybillVo insertByAddBo(LogiPickupWaybillEditBo bo) {
        LogiPickupWaybill add = BeanUtil.toBean(bo, LogiPickupWaybill.class);
        save(add);
        return queryById(add.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiPickupWaybillVo updateByEditBo(LogiPickupWaybillEditBo bo) {
        LogiPickupWaybill update = BeanUtil.toBean(bo, LogiPickupWaybill.class);
        updateById(update);
        return queryById(update.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
